package com.mallify.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mallify.common.model.product.StoreProductAttr;
import com.mallify.service.mapper.StoreProductAttrMapper;
import com.mallify.service.service.StoreProductAttrService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.List;

/**
 * StoreProductAttrServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * | HANSON [ HANSON赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Licensed HANSON并不是自由软件，未经许可不能去掉HANSON相关版权
 * +----------------------------------------------------------------------
 * | Author: HANSON Team <hanson0622@126.com>
 * +----------------------------------------------------------------------
 */
@Service
public class StoreProductAttrServiceImpl extends ServiceImpl<StoreProductAttrMapper, StoreProductAttr>
        implements StoreProductAttrService {

    @Resource
    private StoreProductAttrMapper storeProductAttrMapper;

    /**
     * 根据基本属性查询商品属性详情
     *
     * @param storeProductAttr 商品属性
     * @return 查询商品属性集合
     */
    @Override
    public List<StoreProductAttr> getByEntity(StoreProductAttr storeProductAttr) {
        LambdaQueryWrapper<StoreProductAttr> lqw = Wrappers.lambdaQuery();
        if (null != storeProductAttr.getId()) {
            lqw.eq(StoreProductAttr::getId, storeProductAttr.getId());
        }
        if (StringUtils.isNotBlank(storeProductAttr.getAttrValues())) {
            lqw.eq(StoreProductAttr::getAttrValues, storeProductAttr.getAttrValues());
        }
        if (StringUtils.isNotBlank(storeProductAttr.getAttrName())) {
            lqw.eq(StoreProductAttr::getAttrName, storeProductAttr.getAttrName());
        }
        if (null != storeProductAttr.getProductId()) {
            lqw.eq(StoreProductAttr::getProductId, storeProductAttr.getProductId());
        }
        if (null != storeProductAttr.getType()) {
            lqw.eq(StoreProductAttr::getType, storeProductAttr.getType());
        }
        return storeProductAttrMapper.selectList(lqw);
    }

    /**
     * 删除商品规格
     *
     * @param productId 商品id
     * @param type      商品类型
     * @return Boolean
     */
    @Override
    public Boolean deleteByProductIdAndType(Integer productId, Integer type) {
        LambdaUpdateWrapper<StoreProductAttr> luw = Wrappers.lambdaUpdate();
        luw.set(StoreProductAttr::getIsDel, true);
        luw.eq(StoreProductAttr::getProductId, productId);
        luw.eq(StoreProductAttr::getType, type);
        return update(luw);
    }

    /**
     * 获取商品规格列表
     * @param productId 商品id
     * @param type 商品类型
     * @return List
     */
    @Override
    public List<StoreProductAttr> getListByProductIdAndType(Integer productId, Integer type) {
        LambdaQueryWrapper<StoreProductAttr> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StoreProductAttr::getProductId, productId);
        lqw.eq(StoreProductAttr::getType, type);
        lqw.eq(StoreProductAttr::getIsDel, false);
        return storeProductAttrMapper.selectList(lqw);
    }
}